﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Xml.Serialization;
using System.IO;

namespace SpriteExtractor.Model
{

    public class ImageManager
    {
        Image _image = new Image("");

        public Image Image { get { return _image; } set { _image = value;  } }


        public void SaveSprites(string path)
        {
            Model.Image savingData = Image;
            XmlSerializer serializer = new XmlSerializer(typeof(Model.Image));
            using (var stream = new FileStream(path, FileMode.Create))
            {
                serializer.Serialize(stream, savingData);
            }
        }
        public void LoadSprites(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Model.Image));
            using (var stream = new FileStream(path, FileMode.Open))
            {
                Image = serializer.Deserialize(stream) as Model.Image;
            }
        }

        #region Export 
        public void ExportSprites(string path)
        {
            var output = new StringBuilder();
            output.AppendLine       ("<array>");
            foreach (var sprite in Image.Sprites)
            {
                output.AppendLine   ("    <dict>");
                output.AppendFormat ("        <key>Name</key><string>{0}</string>\n", sprite.Title);
                output.AppendLine   ("        <key>Rect</key>");
                output.AppendLine   ("        <dict>");
                output.AppendFormat ("            <key>X</key><real>{0}</real>\n", sprite.Rect.TopLeft.X);
                output.AppendFormat ("            <key>Y</key><real>{0}</real>\n", sprite.Rect.TopLeft.Y);
                output.AppendFormat ("            <key>Width</key><real>{0}</real>\n", sprite.Rect.Width);
                output.AppendFormat ("            <key>Height</key><real>{0}</real>\n", sprite.Rect.Height);
                output.AppendLine   ("        </dict>");
                output.AppendLine   ("        <key>Anchor</key>");
                output.AppendLine   ("        <dict>");
                output.AppendFormat ("            <key>X</key><real>{0}</real>\n", sprite.Anchor.X);
                output.AppendFormat ("            <key>Y</key><real>{0}</real>\n", sprite.Anchor.Y);
                output.AppendLine   ("        </dict>");
                output.AppendLine   ("    </dict>");
            }
            output.AppendLine       ("</array>");
            File.WriteAllText(path, output.ToString());
        }
        #endregion Export
    }

    public class Image
    {
        public Image() : this("") { }

        public Image(string path)
        {
            Path = path;
        }

        string _path = "";
        public string Path { get { return _path; } set { _path = value ?? ""; } }

        public Size Size { get; set; }

        List<Sprite> _sprites = new List<Sprite>();
        public List<Sprite> Sprites { get { return _sprites; } set { _sprites = value ?? new List<Sprite>(); } }
    }

    public class Sprite
    {
        public string Title { get; set; }
        public Rect Rect { get; set; }
        public Point Anchor { get; set; }

        public Sprite()
        {
            Title = "Untitled";
            Rect = new Rect(20, 20, 70, 70);
            Anchor = new Point(0.5, 0.5);
        }

        public override string ToString()
        {
            return string.Format("{0}, X={1}, Y={2}, Size={3}x{4}, \u2693=({5},{6})"
                , Title, 
                Rect.TopLeft.X, Rect.TopLeft.Y, Rect.Width, Rect.Height, 
                Math.Round(Anchor.X, 2), Math.Round(Anchor.Y, 2));
        }
    }

    
}
